﻿(function () {
    var roamingSettings = Windows.Storage.ApplicationData.current.roamingSettings;
    var roamingFolder = Windows.Storage.ApplicationData.current.roamingFolder;

    function errorHandler(e) {
        var message = new Windows.UI.Popups.MessageDialog(e.message);
        message.showAsync().then(function () {
            WinJS.Navigation.navigate("pages/home/home.html")
        });
    }

    var recordInitialize = function () {
        Record.initRecord();
    }

    var recordSound = function () {
        var recordSwitch = document.getElementById("record-switch").winControl;
        recordSwitch.addEventListener("change", function (event) {
            if (recordSwitch.checked == true) {
                Record.startRecord(roamingSettings.values['keyBind']);
            }
            else {
                Record.stopRecord().then(function () {
                    var submitContent = document.getElementById("submit-content");
                    submitContent.style.display = "block";
                    var player = document.getElementById("player");
                    var keyBind = roamingSettings.values['keyBind'];
                    roamingFolder.getFileAsync(keyBind + ".mp3").then(function (newFile) {
                        player.src = URL.createObjectURL(newFile);
                    });
                });
            }
        })
    };

    var changeSound = function () {
        var newSoundName = document.getElementById("new-sound-name");
        var submitButton = document.getElementById("submit-button"); {
            submitButton.addEventListener("click", function () {
                var keyBind = roamingSettings.values['keyBind'];
                var selectedTile = ViewModels.getInstanceByKeyBind(keyBind);
                roamingFolder.getFileAsync(keyBind + ".mp3").then(function (newFile) {
                    selectedTile.soundSource = URL.createObjectURL(newFile);
                    if (newSoundName.value != "") {
                        selectedTile.soundName = newSoundName.value;
                    }
                    else {
                        selectedTile.soundName = keyBind;
                    }

                    var currentText = JSON.stringify(Data.getSoundTiles());
                    roamingFolder.createFileAsync("last.txt", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (newFile) {
                        Windows.Storage.FileIO.writeTextAsync(newFile, currentText).then(function () {
                            WinJS.Navigation.back();
                        });
                    });
                });
            });
        }
    }

    var deleteSound = function () {
        var discardButton = document.getElementById("discard-button");
        discardButton.addEventListener("click", function (event) {
            WinJS.Application.roaming.remove(roamingSettings.values['keyBind'] + ".mp3");
            WinJS.Navigation.back();
        });
    }

    
    WinJS.Namespace.define("RecordCodeBehind", {
        recordInit: recordInitialize,
        recordSound: recordSound,
        deleteSound: deleteSound,
        changeSound: changeSound
    })
}());